package com.ipet.ad.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ipet.ad.pojo.Ad;
import com.ipet.ad.service.AdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
@Api("营销模块")
public class AdController {
    @Autowired
    private AdService adService;
    @ApiOperation(value = "分页查询所有广告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码" , name = "pageNo" , required = false , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "每一页数据数" , name = "pageSize" , required = false , dataType = "Integer" , paramType = "query"),
    })
    @GetMapping("/addAd")
    public ResponseEntity<PageInfo<Ad>>  findAllAd(@RequestParam(defaultValue = "1") Integer pageNo ,@RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Ad> allAd = adService.getAllAd(pageNo, pageSize);
        if(allAd == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(allAd);
    }
    @ApiOperation(value = "分页条件查询所有广告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "条件集合" , name = "map" , required = false , dataType = "Map" , paramType = "body"),
            @ApiImplicitParam(value = "页码" , name = "pageNo" , required = false , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "每一页数据数" , name = "pageSize" , required = false , dataType = "Integer" , paramType = "query"),
    })
    @PostMapping("/ad")
    public ResponseEntity<PageInfo<Ad>> getAdByCondition(@RequestBody Map<String , Object> map , @RequestParam(defaultValue = "1") Integer pageNo , @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Ad> someAd = adService.getSomeAd(map, pageNo, pageSize);
        if(someAd == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(someAd);
    }
    @ApiOperation(value = "添加或更新对象")
    @ApiImplicitParam(value = "要添加或修改的对象" , name = "ad" , required = true , dataType = "Ad" , paramType = "body")
    @PostMapping("/saveOrUpdate")
    public ResponseEntity<Boolean> saveUpdate(@RequestBody Ad ad){
        boolean saveOrUpdate = adService.saveOrUpdate(ad);
        return ResponseEntity.ok(saveOrUpdate);
    }
    @ApiOperation(value = "删除广告")
    @ApiImplicitParam(value = "要删除的广告id" , name = "id" , required = true , dataType = "Integer" , paramType = "query")
    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> deleteById(Integer id){
        boolean remove = adService.removeById(id);
        return ResponseEntity.ok(remove);
    }
    @GetMapping("/todayAd")
    public List<Ad> getAds(){
        String today = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        List<Ad> adList = adService.list(new QueryWrapper<Ad>().le("start_time", today).ge("end_time", today));
        System.out.println(adList);
        return adList;
    }
}
